<?php

/**
 * This is the model class for table "usuarios".
 *
 * The followings are the available columns in table 'usuarios':
 * @property string $login
 * @property string $nombre
 * @property string $contrasena
 * @property integer $estado
 * @property integer $conectado
 * @property integer $codigointerno
 * @property integer $roles_id
 *
 * The followings are the available model relations:
 * @property Bitacora[] $bitacoras
 * @property Comentarios[] $comentarioses
 * @property Empresas[] $empresases
 * @property Entregas[] $entregases
 * @property Mensajes[] $mensajes
 * @property Tareas[] $tareases
 * @property Roles $roles
 */
class TUsuarios extends CActiveRecord
{
	public $rolActual;
	public $estadoActual;
	public $conecActual;
	/**
	 * Returns the static model of the specified AR class.
	 * @return TUsuarios the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'usuarios';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('login, nombre, contrasena, roles_id', 'required'),
			array('estado, conectado, roles_id', 'numerical', 'integerOnly'=>true),
			array('login', 'length', 'max'=>40),
			array('login','unique'),
			array('nombre', 'length', 'max'=>75),
			array('contrasena', 'length', 'max'=>45),
			array('codigointerno','length','max'=>10),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('login, nombre, contrasena, estado, conectado, codigointerno, roles_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'bitacoras' => array(self::HAS_MANY, 'Bitacora', 'usuarios_login'),
			'comentarioses' => array(self::HAS_MANY, 'Comentarios', 'autor'),
			'empresases' => array(self::HAS_MANY, 'Empresas', 'usuarios_login'),
			'entregases' => array(self::HAS_MANY, 'Entregas', 'usuarios_login'),
			'mensajes' => array(self::HAS_MANY, 'Mensajes', 'para'),
			'tareases' => array(self::HAS_MANY, 'Tareas', 'asignadoa'),
			'roles' => array(self::BELONGS_TO, 'Roles', 'roles_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'login' => 'Login',
			'nombre' => 'Nombre',
			'contrasena' => 'Contrase&ntilde;a',
			'estado' => 'Estado',
			'conectado' => 'Edo. Conexion',
			'codigointerno' => 'Codigo Interno',
			'roles_id' => 'Rol',
			'rolActual'=>'Rol',
			'conecActual'=>'Edo. Conexion',
		
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('login',$this->login,true);
		$criteria->compare('nombre',$this->nombre,true);
		$criteria->compare('contrasena',$this->contrasena,true);
		$criteria->compare('estado',$this->estado);
		$criteria->compare('conectado',$this->conectado);
		$criteria->compare('codigointerno',$this->codigointerno);
		$criteria->compare('roles_id',$this->roles_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public function afterFind(){
		parent::afterFind();
		$r = TRoles::model()->findByPk($this->roles_id);
		$this->rolActual = $r->descripcion;
		if ($this->estado==0){
			$this->estadoActual = "Inactivo";
		}else{
			$this->estadoActual = "Activo";
		}
		if ($this->conectado==0){
			$this->conecActual = "Desconectado";
		}else{
			$this->conecActual = "Conectado";
		}
	}
}